<?php
class UserModel extends CI_Model{
	var $detail;
	
	function __construct(){
		parent::__construct();
	}
	
	/*
	 * check login:
	 */
	function validate_user($username, $password){
		$this->db->from('users');
		$this->db->where('username', $username);
		$this->db->where('password', sha1($password));
		$login = $this->db->get()->result();
		
		if(is_array($login) && count($login) == 1){
			$this->detail = $login[0];
			$this->set_session();
			
			return $login[0];
		}
		return false;
	}
	
	public function validate_user_from_client($username, $password){
		$this->db->from('users');
		$this->db->where('username', $username);
		$this->db->where('password', $password);
		$login = $this->db->get()->result();
		
		if(is_array($login) && count($login) == 1){
			$this->detail = $login[0];
			$this->set_session();
			
			return $login[0];
		}
		return false;
	}
	
	/*
	 * set session when login:
	 */
	function set_session(){
		$this->session->set_userdata(array(
			'id' => $this->detail->_id,
			'role' => $this->detail->role,
			'LoggedIn' => true
		));
	}
	
	/*
	 * get list all user from db:
	 */
	function fetch_users($limit, $start){
		$this->db->limit($limit, $start);
        $query = $this->db->get("users");
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	/*
	 * get number of account:
	 */
	function user_count(){
		return $this->db->count_all("users");
	}
	
	/*
	 * create new user:
	 */
	function create_new_user($userData){	
		return $this->db->insert('users', $userData);
	}
	
	/*
	 * get user by user id
	 */
	function detail_user($user_id){
		//set query to check
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('_id', $user_id);
		
		//run query
		$query = $this -> db -> get();
		if ($query -> num_rows() > 0){
			//set global value and save session
			$result = $query->result();
			return $result[0];
		}else{
			return false;
		}
	}
	
	/*
	 * update user:
	 */
	function update_user($user_id, $user_info){
		$this->db->where('_id', $user_id);
		$this->db->update('users', $user_info);
		$affect = $this->db->affected_rows();

		return $affect;
	}
	
	function delete_user($user_id){
		$this->db->where('_id', $user_id);
		$this->db->delete('users');
	}
	
	public function all_user(){
		//set query to check
		$this->db->select('_id, username, first_name, last_name, date_of_birth, address, phone_number, image, manager_id, role');
		$this->db->from('users');
		
		//run query
		$query = $this -> db -> get();
		if ($query -> num_rows() > 0){
			//set global value and save session
			$result = $query->result();
			foreach ($result as $row) {
                $data[] = $row;
            }
            return $data;
		}else{
			return false;
		}
	}
	
	public function is_exist($username){
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where('username', $username);
		
		$query = $this->db->get();
		
		if ($query->num_rows() == 0){
			return false;
		}
		
		return true;
	}
}